/*
 * Copyright (c) 2017. danlu.com Co.Ltd. All rights reserved.
 */

package cn.com.dhc.danlu.activity.delivery.order.viewholder;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.activity.delivery.order.view.FullScreenReasonPopupView;
import cn.com.dhc.danlu.util.CommonUtils;

import static cn.com.dhc.danlu.util.CommonConst.MyOrderConst.DEAL_COMPLETE;
import static cn.com.dhc.danlu.util.CommonConst.MyOrderConst.HAS_ORDER_REFUND;
import static cn.com.dhc.danlu.util.CommonConst.MyOrderConst.ORDER_CANCLE;
import static cn.com.dhc.danlu.util.CommonConst.MyOrderConst.WAIT_PAY;
import static cn.com.dhc.danlu.util.CommonConst.MyOrderConst.WAIT_RECEIVE;
import static cn.com.dhc.danlu.util.CommonConst.MyOrderConst.WAIT_SEND;
import static cn.com.dhc.danlu.util.CommonConst.MyOrderConst._ORDER_PAYING;

/**
 * author: wuhaiyang(<a href="mailto:wuhaiyang@danlu.com">wuhaiyang@danlu.com</a>)<br/>
 * version: 1.0.0<br/>
 * since: 2017-03-06 下午5:59<br/>
 *
 * <p>
 * 订单详情顶部
 * </p>
 */
public class OrderStatusHeaderWrapper {
    private ImageView ivOrderStatus;
    private TextView tvOrderStatus;
    private TextView tvStatuNote;
    private TextView tvLookReason;

    private String cancelReason;

    public OrderStatusHeaderWrapper(View parentView) {
        initView(parentView);
    }

    private void initView(View parentView) {
        ivOrderStatus = (ImageView) parentView.findViewById(R.id.iv_order_status);
        tvOrderStatus = (TextView) parentView.findViewById(R.id.tv_order_status);
        tvStatuNote = (TextView) parentView.findViewById(R.id.tv_statu_note);
        tvLookReason = (TextView) parentView.findViewById(R.id.tv_look_reason);
        bindAction();
    }

    public void setCancelReason(String cancelReason) {
        this.cancelReason = cancelReason;
    }

    private void bindAction() {
        tvLookReason.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //全屏查看取消原因
                FullScreenReasonPopupView popupView = new FullScreenReasonPopupView(getContext());
                popupView.setCancelReason(cancelReason);
                Activity activity = CommonUtils.getActivity(v);
                if (null != activity) {
                    popupView.showAtLocation(activity.getWindow().getDecorView(), Gravity.NO_GRAVITY, 0, CommonUtils.getStatusBarHeight(activity));
                }
            }
        });
    }

    public void setOrderStatus(String orderStatus, String cancelStatus) {
        if (orderStatus.equals(WAIT_PAY)) {
            // 待付款
            ivOrderStatus.setImageResource(R.drawable.order_wait_pay);
            tvOrderStatus.setText(getContext().getString(R.string.myself_deal_wait_pay));
            tvStatuNote.setText(R.string.note_wait_pay);
            tvLookReason.setVisibility(View.GONE);
        } else if (orderStatus.equals(WAIT_SEND)) {
            // 待发货
            ivOrderStatus.setImageResource(R.drawable.order_wait_send);
            tvOrderStatus.setText(getContext().getString(R.string.myself_deal_wait_send));
            tvStatuNote.setText(R.string.note_wait_send);
            tvLookReason.setVisibility(View.GONE);
        } else if (orderStatus.equals(WAIT_RECEIVE)) {
            // 待收货
            ivOrderStatus.setImageResource(R.drawable.order_wait_receive);
            tvOrderStatus.setText(getContext().getString(R.string.myself_deal_wait_receive));
            tvStatuNote.setText(R.string.note_wait_receive);
            tvLookReason.setVisibility(View.GONE);
        } else if (orderStatus.equals(DEAL_COMPLETE)) {
            // 已完成
            ivOrderStatus.setImageResource(R.drawable.order_completed);
            tvOrderStatus.setText(getContext().getString(R.string.myself_deal_complete));
            tvStatuNote.setText(R.string.note_order_completed);
            tvLookReason.setVisibility(View.GONE);
            tvLookReason.setVisibility(View.GONE);
        } else if (orderStatus.equals(ORDER_CANCLE) || orderStatus.equals(HAS_ORDER_REFUND)) {
            // 已取消 || 已退款
            ivOrderStatus.setImageResource(R.drawable.order_canceled);
            tvOrderStatus.setText(getContext().getString(R.string.order_canceled));
            tvStatuNote.setText(R.string.note_order_canceled);
            tvLookReason.setVisibility(View.VISIBLE);
        } else if (orderStatus.equals(_ORDER_PAYING)) {
            // 支付中
            ivOrderStatus.setImageResource(R.drawable.order_wait_pay);
            tvOrderStatus.setText(getContext().getString(R.string.myself_deal_wait_paying));
            tvStatuNote.setText(R.string.note_order_paying);
            tvLookReason.setVisibility(View.GONE);
        }
        if (cancelStatus.equals("1")) {
            // 取消中
            ivOrderStatus.setImageResource(R.drawable.order_canceling);
            tvOrderStatus.setText(getContext().getString(R.string.order_canceling));
            tvStatuNote.setText(R.string.note_order_cancel_need_confirm);
            tvLookReason.setVisibility(View.VISIBLE);
        }

    }

    private Context getContext() {
        return ivOrderStatus.getContext();
    }
}
